package com.hera.process.config.rest.delegate.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.hera.base.domain.WrapperJSON;
import com.hera.base.rest.delegate.BaseDelegate;
import com.hera.base.util.FiltroDTO;
import com.hera.process.config.rest.delegate.FormDelegate;
import com.hera.process.form.domain.Dynaform;
import com.hera.process.form.domain.DynaformField;
import com.hera.process.form.domain.DynaformStage;
import com.hera.process.form.domain.FieldOption;
import com.hera.process.form.domain.FormField;
import com.hera.process.workflow.domain.Stage;

@Service
public class FormDelegateImpl extends BaseDelegate implements FormDelegate{

	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${url.contexto.form}")
	private String urlController;
	
	@Value("${url.base}")
	private String urlBase;
	
	

	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// DYNAFORM ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<Dynaform> findAllDynaforms(FiltroDTO filtroDTO) {
	
        return postForObject(urlBase+urlController+"dynaform/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<Dynaform>>>() {});
	}

	@Override
	public Dynaform createDynaform(Dynaform dynaform) {
		
	return postForObject(urlBase+urlController+"dynaform/save", dynaform,
        		new ParameterizedTypeReference<WrapperJSON<Dynaform>>() {});
		
	}

	@Override
	public Dynaform updateDynaform(Dynaform dynaform) {
		

		return postForObject(urlBase+urlController+"dynaform/update", dynaform,
        		new ParameterizedTypeReference<WrapperJSON<Dynaform>>() {});
	}

	@Override
	public void deleteDynaform(Dynaform dynaform) {
		
		 postForObject(urlBase+urlController+"dynaform/delete", dynaform,
        		new ParameterizedTypeReference<WrapperJSON<Dynaform>>() {});
	}
	@Override
	public List<Dynaform> findGridFormByIdProcess(Dynaform dynaform){
		return postForObject(urlBase+urlController+"dynaform/listgridformprocess", dynaform,
        		new ParameterizedTypeReference<WrapperJSON<List<Dynaform>>>() {});
	}
	
	@Override
	public List<FormField> findByDynaForm(FiltroDTO filtro) {
		// TODO Auto-generated method stub
		return postForObject(urlBase+urlController+"formfield/list",filtro , 
               new ParameterizedTypeReference<WrapperJSON<List<FormField>>>() {
			});
	}
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// DYNAFORMFIELD ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	@Override
	public List<DynaformField> findAllDynaFormFieldByIdDynaForm(
			FiltroDTO filtro) {
		return postForObject(urlBase+urlController+"dynaformfield/findAllByDynaFormFieldByIdDynaForm", filtro, 
        		new ParameterizedTypeReference<WrapperJSON<List<DynaformField>>>() {});
	}

	@Override
	public List<DynaformField> findAllDynaformsfield(FiltroDTO filtroDTO) {
		return postForObject(urlBase+urlController+"dynaformfield/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<DynaformField>>>() {});
	}

	@Override
	public DynaformField createDynaformfield(DynaformField dynaformfield) {
		return postForObject(urlBase+urlController+"dynaformfield/save", dynaformfield,
        		new ParameterizedTypeReference<WrapperJSON<DynaformField>>() {});
	}

	@Override
	public DynaformField updateDynaformdield(DynaformField dynaformfield) {
		return postForObject(urlBase+urlController+"dynaformfield/update", dynaformfield,
        		new ParameterizedTypeReference<WrapperJSON<DynaformField>>() {});
	}
	
	@Override
	public DynaformField updateDynaformfieldwithFormfield(DynaformField dynaformfield) {
		return postForObject(urlBase+urlController+"dynaformfield/updateformfield", dynaformfield,
        		new ParameterizedTypeReference<WrapperJSON<DynaformField>>() {});
	}	

	@Override
	public void deleteDynaformfield(DynaformField dynaformfield) {
		 postForObject(urlBase+urlController+"dynaformfield/delete", dynaformfield,
	        		new ParameterizedTypeReference<WrapperJSON<DynaformField>>() {});
		
	}
	
	@Override
	public List<DynaformField> findAllByDynaFormField(FiltroDTO filtroDTO) {
		return postForObject(urlBase+urlController+"dynaformfield/findAllByDynaFormField",filtroDTO, 
	               new ParameterizedTypeReference<WrapperJSON<List<DynaformField>>>() {
				});
	}

	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// DYNAFORM STAGE ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/

	@Override
	public List<DynaformStage> findAllDynaformStages(FiltroDTO filtroDTO) {
		
        
        return postForObject(urlBase+urlController+"dynaformstage/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<DynaformStage>>>() {});
	}

	@Override
	public DynaformStage createDynaformStage(DynaformStage dynaformStage) {
		
		return postForObject(urlBase+urlController+"dynaformstage/save", dynaformStage,
        		new ParameterizedTypeReference<WrapperJSON<DynaformStage>>() {});
		
	}

	@Override
	public DynaformStage updateDynaformStage(DynaformStage dynaformStage) {
		
		return postForObject(urlBase+urlController+"dynaformstage/update", dynaformStage,
        		new ParameterizedTypeReference<WrapperJSON<DynaformStage>>() {});
	}
	
	@Override
	public void deleteDynaformStage(DynaformStage dynaformStage) {
		
	
		postForObject(urlBase+urlController+"dynaformstage/delete", dynaformStage,
        		new ParameterizedTypeReference<WrapperJSON<DynaformStage>>() {});
	}
	
	@Override
	public List<DynaformStage> findAllDynaformStagesByStage(Stage stage) {
		
	
        
        return postForObject(urlBase+urlController+"dynaformstagebystage/list", stage,
        		new ParameterizedTypeReference<WrapperJSON<List<DynaformStage>>>() {});
	}
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// FIELD OPTION ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/

	@Override
	public List<FieldOption> findAllFieldOptions(FiltroDTO filtroDTO) {
  
        return postForObject(urlBase+urlController+"fieldoption/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<FieldOption>>>() {});
	}

	@Override
	public FieldOption createFieldOption(FieldOption fieldoption) {
		
	 return postForObject(urlBase+urlController+"fieldoption/save", fieldoption,
	        		new ParameterizedTypeReference<WrapperJSON<FieldOption>>() {});
	}

	@Override
	public FieldOption updateFieldOption(FieldOption fieldoption) {
		
		return postForObject(urlBase+urlController+"fieldoption/update", fieldoption,
	        		new ParameterizedTypeReference<WrapperJSON<FieldOption>>() {});
	}

	@Override
	public void deleteFieldOption(FieldOption fieldoption) {
	
		postForObject(urlBase+urlController+"fieldoption/delete", fieldoption,
        		new ParameterizedTypeReference<WrapperJSON<FieldOption>>() {});
	}

	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// FORMFIELD ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	@Override
	public List<FormField> findAllFormFieldByDynaForm(Long idDynaForm) {
		 return postForObject(urlBase+urlController+"formfield/listByDynaForm", idDynaForm,
	        		new ParameterizedTypeReference<WrapperJSON<List<FormField>>>() {});
	}


	
	@Override
	public List<FormField> findAllFormFields(FiltroDTO filtroDTO) {
		
	
        
        return postForObject(urlBase+urlController+"formfield/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<FormField>>>() {});
	}

	@Override
	public FormField createFormField(FormField formfield) {
		
	
		return postForObject(urlBase+urlController+"formfield/save", formfield,
        		new ParameterizedTypeReference<WrapperJSON<FormField>>() {});
	}

	@Override
	public FormField updateFormField(FormField formfield) {
	

		return postForObject(urlBase+urlController+"formfield/update", formfield,
        		new ParameterizedTypeReference<WrapperJSON<FormField>>() {});
	}

	@Override
	public void deleteFormField(FormField formfield) {
		
	
		postForObject(urlBase+urlController+"formfield/delete", formfield,
        		new ParameterizedTypeReference<WrapperJSON<FormField>>() {});
	}
	
	@Override
	public List<FormField> findAllFormNoAsig(Dynaform dynaform) {
		
        return postForObject(urlBase+urlController+"formDynaformasigno/findAllFormNoasignado", dynaform, 
        		new ParameterizedTypeReference<WrapperJSON<List<FormField>>>() {});
	}
	
	@Override
	public List<FormField> findFormFieldByIdProcess(Dynaform dynaform) {
		
        return postForObject(urlBase+urlController+"formfield/findByIdProcess", dynaform, 
        		new ParameterizedTypeReference<WrapperJSON<List<FormField>>>() {});
	}
		
	@Override
	public List<FormField> findFormFieldNoAsignado(Dynaform dynaform) {
		return postForObject(urlBase+urlController+"formfield/findFormFieldNoAsignado", dynaform, 
        		new ParameterizedTypeReference<WrapperJSON<List<FormField>>>() {});
	}
}
